<?php

function mobile_service_fertilization_info_list($parameters)
{
    global $user;
    $start = $parameters['page_count'] * variable_get("list_page_item_number", 5);

    $query = db_select($parameters['table_name'], 'F');
    $query->condition('F.' . $parameters['pid_name'], $parameters['pid_value']);
    $query->fields('F');

    $query->orderBy('F.timestamp', 'DESC');
    $query->range($start, variable_get("list_page_item_number", 5));
    $results = $query->execute();

    $fertilization_infos = [];
    foreach ($results as $result) {
        $fertilization_infos[] = $result;
    }
    $object_fertilization_infos = (object)["data" => $fertilization_infos];
    return $object_fertilization_infos;
}
function mobile_service_fertilization_info_add($parameters)
{
    global $user;
    try {
        if (isset($parameters['id']) && empty($parameters['id'])) {
            $nid = db_insert($parameters['table_name'])
                ->fields(array(
                    $parameters['pid_name'] => $parameters['pid_value'],
                    'type' => $parameters['type'],
                    'fertilizer_name' => $parameters['fertilizer_name'],
                    'fertilization_date' => $parameters['fertilization_date'],
                    'fertilization_method' => $parameters['fertilization_method'],
                    'fertilizer_quantity' => _empty_2_zero($parameters['fertilizer_quantity']),
                    'comments' => $parameters['comments'],
                    'author_uid' => $user->uid,
                    'timestamp' => time(),
                ))
                ->execute();
            $result_option = (object)["option" => 1];
            return $result_option;

        } else {
            $nid = db_update($parameters['table_name'])
                ->fields(array(
                    'type' => $parameters['type'],
                    'fertilizer_name' => $parameters['fertilizer_name'],
                    'fertilization_date' => $parameters['fertilization_date'],
                    'fertilization_method' => $parameters['fertilization_method'],
                    'fertilizer_quantity' => _empty_2_zero($parameters['fertilizer_quantity']),
                    'comments' => $parameters['comments'],
                ))
                ->condition('id', $parameters['id'], '=')
                ->execute();
            if ($nid > 0) {
                $result_option = (object)["option" => 1];
                return $result_option;
            }
            $result_option = (object)["option" => 0];
            return $result_option;
        }
    } catch (Exception $e) {
        $result_option = (object)["option" => 0, "error" => $e->getMessage()];
        return $result_option;
    }
}